﻿using System;


namespace Assignment3
{
    public interface Shape
    {
        double getArea();
        bool isLegal();
    }

    public class Rectangle : Shape{
        private double width;
        private double length;
        public Rectangle(double w,double l)
        {
            width = w;
            length = l;
        }
        public double getArea()
        {
            return width * length;
        }
        public bool isLegal()
        {
            return width != length&width>0&length>0;
        }
    }

    public class Square : Shape
    {
        private double length;
        public Square(double l)
        {
            length = l;
        }
        public double getArea()
        {
            return length * length;
        }
        public bool isLegal()
        {
            return length>0;
        }
    }

    public class Triangle : Shape
    {
        double side1;
        double side2;
        double side3;
       
        public Triangle(double a,double b, double c)
        {
            side1 = a;
            side2 = b;
            side3 = c;
        }
        public double getArea()
        {
            double p = (side1 + side2 + side3) / 2;
            return Math.Sqrt(p*(p-side1)*(p-side2)*(p-side3));
        }
        public bool isLegal()
        {
            bool c = side1 > 0 & side2 > 0 & side3 > 0;
            return c&side1+side2>side3& side2 + side3 > side1&side1 + side3 > side2;
        }
    }

    public class CreateShape
    {
        public static Shape factory(string s,double a, double b, double c)
        {
            Shape shape;
            switch (s)
            {
                case "rectangle":
                    shape = new Rectangle(a, b);
                    if (shape.isLegal()) return shape;
                    else break;
                case "square":
                    shape =new Square(a);
                    if (shape.isLegal()) return shape;
                    else break;
                    
                case "triangle":
                    shape =new Triangle(a, b, c);
                    if (shape.isLegal()) return shape;
                    else break;
                default:
                    throw new unexistingException("Please input rectangle,square or triangle!");
           }
            throw new unexistingException("Illegal!!!");
        }
        public static Shape randomCreate()
        {
            Random rd = new Random();
            string[] s1 = { "rectangle", "square", "triangle" };
            double a = rd.NextDouble() + rd.Next(1, 3);
            double b = rd.NextDouble() + rd.Next(1, 3);
            double c = rd.NextDouble() + rd.Next(1, 3);
            return factory(s1[rd.Next(0, 3)], a, b, c);
        }
    }

public class unexistingException : Exception
{
    public unexistingException(string message) : base(message)
    {
    }
}
    class Program
    {
        static void Main(string[] args)
        {
            Shape s = new Triangle(3, 4, 5);
            Console.WriteLine("Creating Triangle......");
            if (s.isLegal()) 
            { 
                Console.WriteLine("Sucessful!!! The area of it is "+s.getArea());            
            }
            else Console.WriteLine("Error!!!");

           
                double sumOfArea=0;
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    sumOfArea += CreateShape.randomCreate().getArea();
                }
                catch (unexistingException e)
                {
                    Console.WriteLine(e.Message);
                    sumOfArea = 0;
                    break;
                }
                
            }
            Console.WriteLine(sumOfArea);

        }


    }
    }


